<template>
  <div class="app-container">
    <div class="container">
      <el-row>
        <el-col :span="24" :xs="24">
          <div class="title">传输设备<i class="el-icon-caret-right"></i>实时数据</div>
        </el-col>
      </el-row>
      <!-- 搜索 -->
      <el-row style="margin-left: 20px;">
        <el-col :span="22.5" :xs="24">
          <Search
              :search-data="searchData"
              :search-params="queryParams"
              @change="searchParamsUpdate"
              @selectChange="selectChange"
          >
          </Search>
        </el-col>
        <el-col :span="1.5">
          <el-button
              style="border: 1px solid rgba(0,231,231,0.4);background: rgba(0,231,231,0.051);color: #ffffff;"
              size="mini"
              @click="refresh"
          >刷新</el-button>
        </el-col>
      </el-row>
      <!--    增加-->
      <el-row :gutter="10">
        <el-col :span="1">
          <right-toolbar :showSearch.sync="showSearch" :columns="columns"></right-toolbar>
        </el-col>
      </el-row>
      <!--      表格-->
      <el-table
          :data="tableData"
          height="66vh"
          style="width: 100%;background: #002127;"
          :cell-style="{backgroundColor: '#002127',color: '#ffffff',}"
          :header-cell-style="{ backgroundColor: '#00484D',textAlign: 'center',fontWeight: 'bold',color: '#ffffff',borderBottom: '1px solid #00484D' }">
        <el-table-column prop="deviceId" label="设备ID" align="center" v-if="columns[0].visible"></el-table-column>
        <el-table-column prop="deviceName" label="设备名称" align="center" v-if="columns[1].visible"></el-table-column>
        <el-table-column prop="channelId" label="通道ID" align="center" v-if="columns[2].visible"></el-table-column>
        <el-table-column prop="channel" label="通道名称" align="center" v-if="columns[3].visible"></el-table-column>
        <el-table-column prop="value" label="值" align="center" v-if="columns[4].visible"></el-table-column>
        <el-table-column prop="time" label="上报时间" align="center" v-if="columns[5].visible" show-overflow-tooltip sortable></el-table-column>
      </el-table>
      <!--    分页-->
      <el-row>
        <el-col :span="24" :xs="24">
          <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.page"
              :limit.sync="queryParams.limit"
          />
        </el-col>
      </el-row>
    </div>

  </div>
</template>

<script>
import {getActualByPage, getRuleAllList, getDtuAllList} from '@/api/automation'
import Search from '@/components/PageSearch'
import Pagination from "@/components/Pagination";
import RightToolbar from '@/components/RightToolbar'
export default {
  name: "actual",
  components: { Search, Pagination, RightToolbar },
  data() {
    return {
      // 搜索
      searchData: [
        {
          type: 'select',
          label: '设备',
          prop: 'deviceId',
          options: [],
          placeholder: '请选择设备筛选'
        },
        {
          type: 'select',
          label: '通道',
          prop: 'channelId',
          options: [],
          placeholder: '请选择通道筛选'
        }
      ],
      // 分页总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 20,
        deviceId: '',
        channelId: ''
      },
      // 表格
      tableData: [],
      // 显示搜索条件
      showSearch: true,
      // 列信息
      columns: [
        { key: 0, label: `设备ID`, visible: true },
        { key: 1, label: `设备名称`, visible: true },
        { key: 2, label: `通道ID`, visible: false },
        { key: 3, label: `通道名称`, visible: true },
        { key: 4, label: `值`, visible: true },
        { key: 5, label: `上报时间`, visible: true }
      ],
      dtuOption: [],
      channelOption: []
    }
  },
  props: {},
  watch: {
    dtuOption: {
      handler(val) {
        this.searchData[0].options = this.dtuOption
      },
      immediate: true
    },
    channelOption: {
      handler(val) {
        this.searchData[1].options = this.channelOption
      },
      immediate: true
    },
  },
  created() {
    this.getDtuOption()
    this.getList()
  },
  mounted() {
  },
  methods: {
    //设备下拉
    getDtuOption() {
      getDtuAllList().then(res => {
        const data = res.data
        this.dtuOption = data.map(item => {
          return {
            value: item.id,
            label: item.name
          }
        })
      }).catch(err => {
        console.log(err)
      })
    },
    //通道下拉
    getChannelOption() {
      getRuleAllList(this.queryParams.deviceId).then(res => {
        const data = res.data
        this.channelOption = data.map(item => {
          return {
            value: item.id,
            label: item.name
          }
        })
      }).catch(err => {
        console.log(err)
      })
    },
    selectChange(val, item) {
      console.log('selectChange', val, item)
      if(item == 'deviceId'){
        this.queryParams.channelId = ''
        this.getChannelOption()
      }
    },
    getList() {
      getActualByPage(this.queryParams).then((res) => {
        console.log(res)
        this.total = res.dataCounts
        this.tableData = res.data || []
      }).catch((err) => {
        console.log(err)
        // this.$message.error(err.errorMsg)
      })
    },
    // 新的搜索内容
    searchParamsUpdate() {
      this.getList()
    },
    // 刷新
    refresh(){
      this.getList()
    }
  }
}
</script>

<style lang='less' scoped>
.app-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background: #0C323A;
  .container {
    width: 100%;
    height: 100%;
    background: #002127;
    border-radius: 10px;
    .title {
      height: 42px;
      line-height: 42px;
      border-bottom: 1px solid #999999;
      color: #ffffff;
      font-size: 14px;
      margin-bottom: 15px;
      padding-left: 20px;
      margin-top: 10px;
    }
    // 显隐列
    .el-col-1 {
      float: right;
    }
  }
}
// 表格设置鼠标经过时背景颜色
::v-deep.el-table tbody tr:hover>td{
  background-color: #02565c !important;
}
//表格改变每行底边框颜色
::v-deep.el-table  td, .el-table th.is-leaf  {
  border-bottom:  1px solid rgba(0,231,231,0.302)!important;
}
/*table边框颜色*/
.el-table--border:after,
.el-table--group:after,
.el-table:before {
  background-color: #002127;
}
</style>

